#pragma once

#include "Afxtempl.h"
#include "type.h"
#include "netusb.h"

enum wmstate{WM_INIT, WM_REG, WM_ACTIVE, WM_IDLE, WM_INACTIVE};
enum srvstate{SRV_WAIT, SRV_REGISTER, SRV_RESPOND, SRV_DATA};
#pragma  pack(push)  
#pragma  pack(1)
typedef struct 
{
	u8 		cmd;
	u16		did;
	u16		sid;
}frame;


//#define	DEBUG_WM

#pragma   pack(pop)



#define	CMD				(0)
#define	NTC				(1)
#define	RSP				(2)

#define	REG_TM			(1)
#define	REG_REQ		(2)
#define	REG_REP		(3)
#define	REG_CNL		(4)
#define	CHK_TM			(5)
#define	CHK_REP		(6)
#define	DATA_CMD		(7)
#define	REP_CMD		(8)
#define	DATA_REP		(9)
#define	DATA_FNL		(10)
#define	BOOK_REP		(11)

#define	CMD_TYPE(a, b)	((u8)((a<<5)|(b)))


#define	REG_TMO		(300000)
#define	CHK_TMO		(12)
#define	DATA_TMO		(20)

#define	ITEM_H	(0)
#define	ITEM_M		(1)
#define	ITEM_L	(2)



class Cusb_wmDlg;

class CWmClient
{
// Attributes
public:
	u16 				m_addr;		/*wireless TM address*/
	s32 				m_tm;				/**/
	enum wmstate	m_cstate;
	
	u8				m_buf[10240];	/*save the rx data*/
	u32				m_bufindex;
	bool				m_over;
	bool				m_actc;	/*has communication in this cycle*/
// Operations
public:
	CWmClient();
	virtual ~CWmClient();
};

typedef CList<CWmClient, CWmClient&> 	CWmClientList;

// CWmDrv

class CWmDrv
{
public:
	u32 				m_rxad;
	u32 				m_txad;
	u32 				m_RexMax;		/*Max number of register, 30 default*/
	u32				m_DataTmo;		/*data transfer time out*/
	u32				m_RegSlice;		/*register time out*/
	u32				m_DataSlice;		/*data time out*/
	u32			 	m_ClientSlice[3];	/*request chek tick for each client*/
	u32				m_RetryMax;	/*retry times in data transfer*/
	u32				m_idle;			/*H->M time*/

	CWmClientList	 	m_ClientList[3];
	u32			 	m_ClientNum[3];
	HANDLE        		m_hEvent;
	enum srvstate       m_sstate; 
	bool				m_newlogin;	
	bool				m_newrequst;	
	bool				m_tmo;	
	u16 				m_serid;
	CWmClient*		m_client;		/*current client*/
	//CWmSocket* 		m_pSocket;
	CWinThread*		m_pThread;
	u32 				m_exit;
	Cusb_wmDlg* 	m_pDlg;
	CFile* 			m_pLogFile;
	bool				m_pause;
	bool				m_atomlist;

	void StartUSBwm();
	void StopUSBwm();
	void PauseUSBwm();

	void OnReceiveUSBMSG (WPARAM wParam, LPARAM lParam);
	void TxRegisterNotice(void); 
	void TxRegisterResult(void); 
	void TxCheckNotice(void); 
	void TxDataRequest(void); 
	void TxQuickRespond(void); 
	void RxClientRegister(BYTE* rb); 
	void RxClientRespond(BYTE* rb); 
	void RxClientData(BYTE* rb, u32 n); 
	void TxtoSocket(void); 
	void RxFromSocket(void); 
	void GetIdleClient(u16 wmid); 
	void FreshList(void); 
	bool CheckAndAdd(u16 wmid); 
	bool CheckAndDelete(u16 wmid); 
	void LogFrame(char * fn, frame fm, u32 n);
	void SetParameters(u32 RexMax, u32 ClientNum[], u32 ClientSlice[], u32 DataTmo,
					u32 RegSlice, u32 DataSlice,  u32 RetryMax, u32 idle); 
	
public:

	CWmDrv(u32 rxad, u32 txad, u32 baud, u32 id,Cusb_wmDlg* pDlg,
					u32 RexMax, u32 ClientNum[], u32 ClientSlice[], u32 DataTmo,
					u32 RegSlice, u32 DataSlice,  u32 RetryMax, u32 idle
					);		
	CWmDrv();
	           // protected constructor used by dynamic creation
	virtual ~CWmDrv();


};



